package com.fy.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fy.mapper.SysFunctionMapper;
import com.fy.pojo.entity.SysFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Slf4j
@Component
public class SysFctionCountUtils {

    @Autowired
    private SysFunctionMapper sysFunctionMapper;

    private static SysFunctionMapper staticSysFunctionMapper;

    @PostConstruct
    public void init() {
        staticSysFunctionMapper = this.sysFunctionMapper;
    }

    public static void sysFctionAddCount(String title, String date) {
        log.info("数据:{},{}",title,date);
        LambdaQueryWrapper<SysFunction> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysFunction::getDate,date);
        List<SysFunction> list = staticSysFunctionMapper.selectList(wrapper);
        if (list.isEmpty()) {
            SysFunction sysFunction=new SysFunction();
            sysFunction.setTitle(title);
            sysFunction.setCount(1);
            sysFunction.setDate(date);
            staticSysFunctionMapper.insert(sysFunction);
        } else {
            LambdaUpdateWrapper<SysFunction> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SysFunction::getTitle, title)
                    .eq(SysFunction::getDate, date)
                    .setSql("count = count + 1");
            staticSysFunctionMapper.update(null, updateWrapper);
        }
    }
}